package EventPackage;

public class SensorEventManagerInterface extends EventManagerInterface {

    public static final int MONITOR_EXISTS_EVENT = 70;
    public static final int REGISTER_EV_ID = -70;
    public static final int KEEP_ALIVE_EV_ID = -71;

    private String name;
    private String description;
    private int keep_alive_report_time;

    private boolean is_reporting_keep_alives = false;
    private KeepAliveDispatcher keep_alive_dispatcher = null;

    public SensorEventManagerInterface(int retry_interval, int time_to_live, String name, String description, int keep_alive_report_time) throws LocatingEventManagerException, RegistrationException, ParticipantAlreadyRegisteredException {
        super(retry_interval, time_to_live);
        this.name = name;
        this.description = description;
        this.keep_alive_report_time = keep_alive_report_time;
        this.sendRegisterEvent();
    }

    public SensorEventManagerInterface(String ServerIpAddress, int retry_interval, int time_to_live, String name, String description, int keep_alive_report_time) throws LocatingEventManagerException, RegistrationException, ParticipantAlreadyRegisteredException {
        super(ServerIpAddress, retry_interval, time_to_live);
        this.name = name;
        this.description = description;
        this.keep_alive_report_time = keep_alive_report_time;
        this.sendRegisterEvent();
    }

    public SensorEventManagerInterface(String name, String description, int keep_alive_report_time) throws LocatingEventManagerException, RegistrationException, ParticipantAlreadyRegisteredException {
        super();
        this.name = name;
        this.description = description;
        this.keep_alive_report_time = keep_alive_report_time;
        this.sendRegisterEvent();
    }

    public SensorEventManagerInterface(String ServerIpAddress, String name, String description, int keep_alive_report_time) throws LocatingEventManagerException, RegistrationException, ParticipantAlreadyRegisteredException {
        super(ServerIpAddress);
        this.name = name;
        this.description = description;
        this.keep_alive_report_time = keep_alive_report_time;
        this.sendRegisterEvent();
    }

    private void sendRegisterEvent() {
        try {
            super.SendEvent(new Event(REGISTER_EV_ID,this.name+"|"+this.description));
        } catch (Exception e) {
            if (EventManagerInterface.mw != null) {
                EventManagerInterface.mw.WriteMessage("Error sending register event: "+e.getMessage());
            }
        }
    }

    @Override
    public EventQueue GetEventQueue() throws ParticipantNotRegisteredException, GetEventException {
        EventQueue evQueue = super.GetEventQueue();

        if (!this.is_reporting_keep_alives) {
            if (evQueue != null) {
                EventQueue copy = evQueue.GetCopy();
                Event evt;
                while ((evt = copy.GetEvent()) != null) {
                    // there is a monitor, start sending keep alive
                    if (evt.GetEventId() == SensorEventManagerInterface.MONITOR_EXISTS_EVENT) {
                        if (EventManagerInterface.mw != null) {
                            EventManagerInterface.mw.WriteMessage("Maintenance Monitor detected. Start sending keep alive.");
                        }
                        this.sendRegisterEvent();
                        this.keep_alive_dispatcher = new KeepAliveDispatcher(this.dispatcher, this.keep_alive_report_time, this.ParticipantId, this.name, this.description);
                        this.keep_alive_dispatcher.start();
                        this.is_reporting_keep_alives = true;
                        break;
                    }
                }
            }
        }

        return evQueue;
    }

    public void stop() {
        if (this.keep_alive_dispatcher != null) {
            this.keep_alive_dispatcher.setRunning(false);
        }
        super.stop();
    }

}
